package com.atguigu.lease.web.app.custom.interceptor;


import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取request中的请求头token
        String token = request.getHeader("access_token");
        //  判断是否为空
        if (ObjectUtils.isEmpty(token)) {
            throw new LeaseException(ResultCodeEnum.APP_LOGIN_AUTH.getMessage(),ResultCodeEnum.APP_LOGIN_AUTH.getCode());
            //解释token 进行判定
        } else {
            JwtUtil.parseToken(token);
        }
        return true;
    }


}